#include "stdafx.h"
#include "ListDefine.h"

namespace DuiLib
{

double CalculateDelay(double state)
{
    return pow(state, 2);
}

void Node::SetParent(Node* parent)
{
	_parent = parent;
}

Node::Node()
	: _parent(NULL)
{}

Node::Node(NodeData t)
	: _data(t)
	, _parent(NULL)
{}

Node::Node(NodeData t, Node* parent)
: _data(t)
, _parent(parent)
{}

Node::~Node() 
{
	for (int i = 0; i < GetChildrenNumber(); ++i)
		delete _children[i]; 
}

NodeData& Node::data()
{
	return _data;
}

int Node::GetChildrenNumber() const
{
	return static_cast<int>(_children.size());
}

Node* Node::child(int i)
{
	return _children[i];
}

Node* Node::parent()
{
	return ( _parent);
}

bool Node::hasChildren() const
{
	return GetChildrenNumber() > 0;
}

bool Node::folder() const
{
	return _data.folder;
}

void Node::AddChild(Node* child)
{
	child->SetParent(this); 
	_children.push_back(child); 
}


void Node::RemoveChild(Node* child)
{
	Children::iterator iter = _children.begin();
	for (; iter < _children.end(); ++iter)
	{
		if (*iter == child) 
		{
			_children.erase(iter);
			return;
		}
	}
}

Node* Node::GetLastChild()
{
	if (hasChildren())
	{
		return child(GetChildrenNumber() - 1)->GetLastChild();
	}
	return this;
}

std::vector<Node*>* Node::GetChildren()
{
	return &_children;
}

}